package com.wo.android.pockybill;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.os.Bundle;
import android.text.InputType;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.wow.wa.bill.serivce.BillService;
import com.wow.wa.bill.serivce.BillServiceImpl;
import com.wow.wa.bill.util.Args;
import com.wow.wa.bill.vo.Card;
import com.wow.wa.ui.CategoryBtn;
import com.wow.wa.ui.CategoryType;
import com.wow.wa.ui.bean.UICategory;

public class EditDetialActivity extends BaseActivity {

	private CategoryBtn name;
	
	private CategoryBtn amount;
	private CategoryBtn note;
	private CategoryBtn type;

	private Boolean isShowEdit = true;
	private Menu menu;
	private Button updateBtn;
	private Button reButton;
	private BillService service;

	@Override
	public Class<? extends BaseActivity> toWhenError() {
		// TODO Auto-generated method stub
		return (Class<? extends BaseActivity>) com.wo.android.pockybill.util.Args.Activity.homeActivity;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_card_view);
		this.getActionBar().setDisplayHomeAsUpEnabled(true);
		this.service = new BillServiceImpl(this);

		this.name = (CategoryBtn) this.findViewById(R.id.name);
		
		this.amount = (CategoryBtn) this.findViewById(R.id.amount);
		this.note = (CategoryBtn) this.findViewById(R.id.note);
		this.type = (CategoryBtn) this.findViewById(R.id.type);

		this.updateBtn = (Button) this.findViewById(R.id.updateBtn);
		this.reButton = (Button) this.findViewById(R.id.reBtn);

		final Card card = (Card) this.getIntent().
				getSerializableExtra(
				Args.DBSchema.CARD_TABLE_NAME);

		

		this.note.createEditDialog(this.getString(R.string.note),
				card.getNote(), false, InputType.TYPE_CLASS_TEXT);

		this.amount.createEditDialog(this.getString(R.string.allMoney),
				card.getAmount(), false, InputType.TYPE_CLASS_NUMBER);

		this.name.createEditDialog(this.getString(R.string.name),
				card.getName(), false, InputType.TYPE_CLASS_TEXT);

		this.type.createCategoryDialog(this.getString(R.string.cardType),
				this.getPayType(), true, card.getType());

		

		this.updateBtn.setVisibility(View.GONE);

		this.updateBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				card.setAmount(amount.getBeanValue());
				card.setName(name.getBeanValue());
				card.setType(type.getBeanValue());
				service.updateCard(card, EditDetialActivity.this);
				setResult(Args.Common.EDIT_CARD_CODE);
				finish();
			}
		});

		this.reButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Card card = (Card) getIntent().getSerializableExtra(
						Args.DBSchema.CARD_TABLE_NAME);
				name.setCategory(card.getName(), card.getName());
				
				amount.setCategory(card.getAmount(), card.getAmount());
				type.setCode(card.getType());

			}
		});

	}

	private List<UICategory> getPayType() {
		final List<UICategory> isPayCategorys = new ArrayList<UICategory>();
		isPayCategorys.add(new UICategory(Args.Common.STRING_ZERO, this
				.getString(R.string.oneTypePay)));
		isPayCategorys.add(new UICategory(Args.Common.STRING_ONE, this
				.getString(R.string.monthTypePay)));
		return isPayCategorys;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem(R.id.action_edit).setVisible(isShowEdit);
		menu.findItem(R.id.action_view).setVisible(!isShowEdit);
		this.menu = menu;
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.card, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			this.finish();
			break;
		case R.id.action_edit:
			this.amount.bindOnClickEvent();
			this.name.bindOnClickEvent();
			
			this.note.bindOnClickEvent();
			this.type.bindOnClickEvent();
			isShowEdit = false;

			menu.findItem(R.id.action_edit).setVisible(isShowEdit);
			menu.findItem(R.id.action_view).setVisible(!isShowEdit);

			this.updateBtn.setVisibility(View.VISIBLE);
			break;

		case R.id.action_view:
			isShowEdit = true;
			this.amount.unBindOnClickEvent();
			this.name.unBindOnClickEvent();
			
			this.note.unBindOnClickEvent();
			this.type.unBindOnClickEvent();

			menu.findItem(R.id.action_edit).setVisible(isShowEdit);
			menu.findItem(R.id.action_view).setVisible(!isShowEdit);

			this.updateBtn.setVisibility(View.GONE);

			break;
		default:
			break;
		}
		return true;
	}

}
